package util

import (
	"fmt"
	"net"
	"strconv"
	"strings"
)
import "github.com/malfunkt/iprange"

func ParseIpList(ipStr string) ([]net.IP, error) {
	list, err := iprange.ParseList(ipStr)
	if err != nil {
		return nil, err
	}

	return list.Expand(), nil
}

func ParsePorts(portRanges string) ([]int, error) {
	var portList []int

	ranges := strings.Split(portRanges, ",")
	for _, r := range ranges {
		ends := strings.Split(r, "-")
		if len(ends) > 1 {
			start, err := strconv.Atoi(ends[0])
			if err != nil {
				return nil, fmt.Errorf("Error converting start:", err)
			}
			end, err := strconv.Atoi(ends[1])
			if err != nil {
				return nil, fmt.Errorf("Error converting end:", err)
			}
			for i := start; i <= end; i++ {
				portList = append(portList, i)
			}
		} else {
			i, err := strconv.Atoi(ends[0])
			if err != nil {
				return nil, fmt.Errorf("Error converting start:", err)
			}
			portList = append(portList, i)
		}
	}

	return portList, nil
}
